﻿Public Class importerDysAct


    Dim db As New Connexion
    Dim action As String
    Dim Entrer, Entrer2 As String
    Dim TextBoxID As String
    Dim Niveau As Integer = 1
    Dim LibelleNiv As String()
    Dim CodeNiv As String()
    Dim NomNiv As String()
    Dim IdNiv As String()
    Dim Nomtable As String()
    Dim NbrNiv As Integer = 0
    Public idAct As Integer
    Public databaseName As String
    Private Sub importerDysAct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Entrer = ""
        DataGridView1.MultiSelect = False
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView1.ReadOnly = True
        Entrer2 = ""
        DataGridView2.MultiSelect = False
        DataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView2.ReadOnly = True



        db.myReq = "SELECT COUNT(syscolumns.name) AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'DysfonTypes'"
        db.Connecter(databaseName)
        While db.rdr.Read()
            NbrNiv = db.rdr(0) - 6
        End While
        db.Disconnecter()
        MsgBox("Intial" & NbrNiv + 6)
        LibelleNiv = New String(NbrNiv - 1) {}
        CodeNiv = New String(NbrNiv - 1) {}
        NomNiv = New String(NbrNiv - 1) {}
        IdNiv = New String(NbrNiv - 1) {}
        Nomtable = New String(NbrNiv + 6) {}
        Dim iter As Integer = 1
        While iter < NbrNiv
            db.myReq = "SELECT [Libelle_Niveau] FROM  [dbo].[Niveau] Where [Table_Niveau]='DysfonTypes' and [Niv_Niveau]='" & iter & "'"
            db.Connecter(databaseName)
            While db.rdr.Read()
                LibelleNiv(iter - 1) = db.rdr(0)
            End While
            db.Disconnecter()
            iter = iter + 1
        End While

        db.myReq = "SELECT syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'DysfonTypes'"
        db.Connecter(databaseName)
        iter = 0
        While db.rdr.Read()
            Nomtable(iter) = db.rdr(0)
            iter = iter + 1
        End While
        db.Disconnecter()

        Niveau = 1
        majTyp()

        majourOrigine()
        majourDest()

    End Sub

    Private Sub majourOrigine()
        Try
            db.myReq = "SELECT * FROM  [dbo].[DysfonSites] where [Id_TypDys]='" & Me.ComboBoxCodeTyp.Text & "'"
            db.Connecter(databaseName)
            Me.DataGridView1.Rows.Clear()
            While db.rdr.Read()
                DataGridView1.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3), db.rdr(4), db.rdr(5), db.rdr(6))
            End While
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub majourDest()
        Try
            db.myReq = "SELECT * FROM  [dbo].[DysfonSite_ActivHumSit] W INNER JOIN   [dbo].[DysfonSites] S ON W.Id_Dys=S.Id_Dys where [Id_Activ]='" & idAct & "'"
            db.Connecter(databaseName)
            Me.DataGridView2.Rows.Clear()
            While db.rdr.Read()
                DataGridView2.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(6), db.rdr(9))
            End While
            db.Disconnecter()

        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub majTyp()

        Dim requeteX As String
        Dim niv As Integer = 1


        requeteX = "Select * from  [dbo].[DysfonTypes] WHERE " & Nomtable(3 + NbrNiv) & " = '" & Niveau & "'"

        While niv < Niveau
            requeteX = requeteX & "and " & Nomtable(2 + niv) & " = '" & CodeNiv(niv - 1) & "' "
            niv = niv + 1
        End While


        If Niveau = 1 Then
            Me.ButtonBack.Enabled = False
            Me.ButtonNext.Enabled = True
        ElseIf Niveau = NbrNiv Then
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = True
        End If

        db.myReq = requeteX
        db.Connecter(databaseName)

        Me.ComboBoxCodeTyp.Items.Clear()
        Me.ComboBoxNomType.Items.Clear()
        While db.rdr.Read()
            Me.ComboBoxNomType.Items.Add(db.rdr(NbrNiv + 4))
            Me.ComboBoxCodeTyp.Items.Add(db.rdr(0))
        End While
        db.Disconnecter()

        If ComboBoxCodeTyp.Items.Count < 0 Or Niveau = NbrNiv Then
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonNext.Enabled = True
        End If
    End Sub


    Private Sub ComboBoxNomType_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomType.TextChanged

        'If Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text) > 0 Then
        Dim Pos As Int32 = Me.ComboBoxNomType.Text.Length
        Me.ComboBoxNomType.SelectedIndex = Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text)
        Me.ComboBoxNomType.SelectionStart = Pos
        Me.ComboBoxNomType.SelectionLength = Me.ComboBoxNomType.Text.Length - Pos
        Me.ComboBoxCodeTyp.SelectedIndex = Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text)

        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab.Text = ""
        NomNiv(Niveau - 1) = Me.ComboBoxNomType.Text
        While niv < NbrNiv
            Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
            niv = niv + 1
        End While

        IdNiv(Niveau - 1) = Me.ComboBoxCodeTyp.Text
        'Else
        ' Me.ComboBoxCodeTyp.Text = ""
        ' End If
    End Sub


    Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
        Entrer = ""
        Niveau = Niveau + 1
        Me.LabelNiv.Text = LibelleNiv(Niveau)

        db.myReq = "Select * from  [dbo].[DysfonTypes] WHERE " & Nomtable(0) & " = '" & Me.ComboBoxCodeTyp.Text & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            CodeNiv(Niveau - 2) = db.rdr(1 + Niveau)
        End While

        majTyp()
    End Sub
    Private Sub ButtonBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBack.Click
        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab.Text = ""
        Entrer = ""
        Niveau = Niveau - 1
        Me.LabelNiv.Text = LibelleNiv(Niveau - 1)
        While niv < Niveau
            Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
            niv = niv + 1
        End While
        majTyp()
        Me.ComboBoxNomType.SelectedIndex = Me.ComboBoxNomType.Items.IndexOf(IdNiv(Niveau - 1))
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Dim typX As New TypologieNivXssCatg
        typX.Text = "Typologie"
        typX.Table2 = "DysfonTypes"
        typX.Visible = True
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            Entrer = ""

            db.myReq = "INSERT INTO  [dbo].[DysfonSite_ActivHumSit]([Id_Dys],[Id_Activ],[Explic_SrceIS]) VALUES ('" & Me.DataGridView1.SelectedCells(0).Value & "','" & idAct & "','" & Me.TextBoxDist.Text & "')"
            MsgBox(db.myReq)

            db.Connecter(databaseName)
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
        majourDest()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Entrer = ""

            db.myReq = "DELETE FROM  [dbo].[DysfonSite_ActivHumSit] WHERE [Id_Dys]='" & Me.DataGridView2.SelectedCells(0).Value & "' and [Id_Activ]='" & Me.DataGridView2.SelectedCells(1).Value & "'"
            MsgBox(db.myReq)

            db.Connecter(databaseName)
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
        majourDest()
    End Sub
End Class